package stack_queue.leetcode.Leetcode;

/**
 * @author shkstart
 * @create 2022-10-13-12:29
 */
public class MyCircularQueue622 {


    private int[] data;
    private int front;
    private int tail;

    public MyCircularQueue622(int k) {
        data = new int[k + 1];
    }

    public boolean enQueue(int value) {
        if (!isFull()) {
            data[tail] = value;
            tail = (tail + 1) % data.length;
            return true;
        }
        return false;
    }

    public boolean deQueue() {
        if (!isEmpty()) {

            front=(front+1)%data.length;
            return true;
        }
        return false;
    }

    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return data[front];
    }

    public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        //最后一个元素的下标
        int index = tail==0 ? data.length-1:tail-1;
        return data[index];
    }

    public boolean isEmpty() {
        if (front == tail) {
            return true;
        }
        return false;
    }

    public boolean isFull() {
        if (front == (tail + 1) % data.length) {
            return true;
        }
        return false;

    }
}
